<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/7
 * Time: 10:17
 */

namespace app\admin\controller;


use app\admin\Model\CommodityType;
use app\common\model\plugin;
use app\Model\CommodityAttributeModel;
use app\Model\CommodityTypeModel;
use Symfony\Component\Yaml\Dumper;
use think\Db;
use think\Request;
class Attributes extends Common
{

    /*/商品属性首页
     * $list 查询该属性属于什么商品 并按排序字段降序显示
     * type_id 商品类型id
     * $select 查询类型名称
     * */
 public function attributes(){
//     dump(input('type_id'));die();
     $where=[];
     if(input('type_id')){
         $where['c.type_id']=input('type_id');
     }

     $select=Db('commodity_type')->select();
     $list=Db('commodity_attribute')
           ->alias('c')
           ->join('commodity_type t','c.type_id=t.id')
            ->where($where)
           ->order('c.order desc')
           ->paginate();


     $this->assign('select',$select);
     $this->assign('list',$list);
     $this->getConAndAct('Goods');
   return $this->fetch('Attributes');
 }


 /*
  * 属性添加编辑页
  */
    public function add()
    {
        $getData=input('get.');
        //属性类型
       $data['type']= CommodityTypeModel::where('delete_time',null)->select();
       if (isset($getData['attr_id'])){//编辑
           $data['attr']=CommodityAttributeModel::find($getData['attr_id']);
           return view('edit',$data);
       }else{
           return view('add',$data);

       }

    }

   /*
    * 属性添加编辑
    */
    public function addData()
    {
        $getData=input('post.');
        if ($getData['type_id']==0){
            return retJson('',201,'请选择类型');
        }
        if (isset($getData['attr_id'])){//编辑
            sole('commodity_attribute',['attr_name'=>$getData['attr_name'],'type_id'=>$getData['type_id']],['attr_id',$getData['attr_id']],'该类型已有属性');//判断修改账号是否存在
            $setCommAttr=CommodityAttributeModel::update($getData);
        }else{//新增
            sole('commodity_attribute',['attr_name'=>$getData['attr_name'],'type_id'=>$getData['type_id']],[],'该类型已有属性');//判断修改账号是否存在
            $setCommAttr= CommodityAttributeModel::insert($getData);
        }
        return setDatabase($setCommAttr);
   }
 /*
  * 商品属性删除
  *
  */

 public function del(){
     $id=input('id');
     $res=Db('commodity_attribute')->where('attr_id',$id)->delete();
     if ($res){
         return retJson('',200,'删除成功');
     }else{
         return retJson('',201,'删除失败');
     }
 }

 /**
   商品属性筛选
  */
public function AttributesSearch(){
    $id=input('type_id');
    $where=[];
    if ($id){$where['type_id']=$id;}
    $select=Db('commodity_type')->select();
    $list=Db('commodity_attribute')
        ->alias('c')
        ->where($where)
        ->join('commodity_type t','c.type_id=t.id')
        ->order('order','desc')
        ->paginate(null,false,['query' => request()->param() ]);
    $this->assign('select',$select);
    $this->assign('list',$list);
    return $this->fetch('Attributes');
}
}